package Classic150;

import java.util.Stack;

public class 有效的括号 {
    /*
        栈的应用，注意存入的值的巧变
     */
    public boolean isValid(String s) {
        if (s.length() % 2 != 0) return false;
        Stack<Character> stack = new Stack<>();
        for (char c : s.toCharArray()) {
            if (c == '(' || c == '{' || c == '[') stack.push(c == '(' ? ')' : c == '{' ? '}' : ']');
            else {
                if (!stack.isEmpty() && stack.peek() == c) stack.pop();
                else return false;
            }
        }
        return stack.isEmpty();
    }
}
